/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.web;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.javaeye.delivery.dto.DeliveryOrder;
import com.javaeye.delivery.dto.DeliveryOrderDetail;
import com.javaeye.delivery.service.IDeliveryOrderService;
import com.javaeye.common.web.PageInfo;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.ExcelUtil;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.util.ListItem;
import com.javaeye.common.util.PathUtil;

import com.opensymphony.xwork2.ActionSupport;

public class DeliveryOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private static final String REPORT_FILE = "deliveryOrder";
	
	private OrderCondition condition;
	
	private PageInfo pageInfo;
	
	private List<DeliveryOrder> orderList;
	
	private String orderId;
	
	private List<ListItem> logisticsTypeList;
	
	/**
	 * 成品交运单信息
	 */
	private DeliveryOrder order;
	
	/**
	 * 成品交运单明细ID
	 */
	private List<Integer> detailIds;
	
	private InputStream excelStream;
	
	/**
	 * 成品交运单明细产品备注
	 */	
	private List<String> deliveryInfos;
	
	private IDeliveryOrderService service;
	
	public DeliveryOrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new OrderCondition();
		condition.setOrderNo("");
		condition.setCustomName("");
		condition.setProductName("");
		condition.setLogisticsName("");
	}
	
	/**
	 * 查询成品交运单列表
	 * @return
	 * @throws Exception
	 */
	public String queryOrderList() throws Exception {
		orderList = service.getOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询成品交运单信息
	 * @return
	 * @throws Exception
	 */
	public String queryDeliveryOrderInfo() throws Exception {
		order = service.getDeliveryOrder(orderId);
		return SUCCESS;
	}
	
	public String queryOrderForEdit() throws Exception {
		logisticsTypeList = ListUtil.logisticsTypeList();
		return queryDeliveryOrderInfo();
	}
	
	public String reportOrderInfo() throws Exception {
		DeliveryOrder order = service.getDeliveryOrder(orderId);
		File file = PathUtil.getResource(".." + File.separator + PathUtil.REPORT_DIR + File.separator + REPORT_FILE + ExcelUtil.EXCEL_SUFF);
		Workbook book = ExcelUtil.createWorkBook(file);
		configReport(order, book);
		excelStream = ExcelUtil.workbook2InputStream(book);
		return SUCCESS;
	}
	
	/**
	 * 保存成品交运单
	 * @return
	 * @throws Exception
	 */
	public String saveOrder() throws Exception {
		if (1 == Integer.parseInt(order.getLogisticsType())) {
			if (order.getLogisticsId() == 0) {
				this.addActionError("无法保存，公司合约货运，就必须选择物流公司！");
				queryOrderForEdit();
				return INPUT;
			}
		}
		DeliveryOrder dbOrder = service.getDeliveryOrder(orderId);
		dbOrder.setOrderNo(order.getOrderNo());
		dbOrder.setDeliveryDate(order.getDeliveryDate());
		dbOrder.setReceivers(order.getReceivers());
		dbOrder.setTelephone(order.getTelephone());
		dbOrder.setAddress(order.getAddress());
		dbOrder.setTotleAmount(order.getTotleAmount());
		dbOrder.setLogisticsType(order.getLogisticsType());
		dbOrder.setLogisticsId(order.getLogisticsId());
		dbOrder.setLogisticsName(order.getLogisticsName());
		dbOrder.setTotleWeight(order.getTotleWeight());
		dbOrder.setTotleVolume(order.getTotleVolume());
		
		Set<DeliveryOrderDetail> details = dbOrder.getOrderDetails();
		int i = 0;
		for (DeliveryOrderDetail detail : details) {
			detail.setDeliveryInfo(deliveryInfos.get(i));
			i++;
		}
		service.save(dbOrder);
		return SUCCESS;
	}
	
	private void configReport(DeliveryOrder order, Workbook book) {
		Sheet sheet =  book.getSheetAt(0);
		int rolIndex = 1, orderColIndex1 = 1, orderColIndex2 = 6;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getOrderNo());
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex2, DateUtils.formatString(order.getDeliveryDate(), "yyyy-MM-dd"));
		rolIndex++;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getCustomName());
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex2, order.getTotleAmount());
		rolIndex++;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getReceivers());
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex2, order.getTelephone());
		rolIndex++;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getAddress());
		rolIndex++;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getLogisticsTypeName());
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex2, order.getLogisticsName());
		rolIndex++;
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex1, order.getTotleWeight());
		ExcelUtil.setCellValue(sheet, rolIndex, orderColIndex2, order.getTotleVolume());
		rolIndex++;
		int colIndex = 0;
		Set<DeliveryOrderDetail> details = order.getOrderDetails();
		for (DeliveryOrderDetail detail : details) {
			rolIndex++;
			colIndex = 0;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getProductRootCategory());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getProductCategory());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getProductName());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getProductType());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getProductSku());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getUnit());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getNumber());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getPackageNumber());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getPrice());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getAmount());
			colIndex++;
			ExcelUtil.setCellValue(sheet, rolIndex, colIndex, detail.getDeliveryInfo());
		}
	}

	public OrderCondition getCondition() {
		return condition;
	}

	public void setCondition(OrderCondition condition) {
		this.condition = condition;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<DeliveryOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<DeliveryOrder> orderList) {
		this.orderList = orderList;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public DeliveryOrder getOrder() {
		return order;
	}

	public void setOrder(DeliveryOrder order) {
		this.order = order;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<String> getDeliveryInfos() {
		return deliveryInfos;
	}

	public void setDeliveryInfos(List<String> deliveryInfos) {
		this.deliveryInfos = deliveryInfos;
	}

	public void setService(IDeliveryOrderService service) {
		this.service = service;
	}
	
	public List<ListItem> getLogisticsTypeList() {
		return logisticsTypeList;
	}
	
	public void setLogisticsTypeList( List<ListItem> logisticsTypeList) {
		this.logisticsTypeList = logisticsTypeList;
	}
	
	public InputStream getExcelStream() {
		return excelStream;
	}

	public void setExcelStream(InputStream excelStream) {
		this.excelStream = excelStream;
	}
	
	public String getFileName() {
		String now = DateUtils.dateToString("yyyyMMddHHmmss");
		return REPORT_FILE + "_" + now;
	}
}
